package com.yellowpage.ndc.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ndc.data.Address;
import com.yellowpage.ndc.service.AddressService;

public class AddressAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<Address> addressList = new ArrayList<Address>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private Address address;
	private AddressService addressService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_addrId;
		private String query_regionCode;
		private String query_addr;
		private String query_addrBrief;
		private String query_addrNum;
		private String query_addrLast;
		private String query_zip;
		private String query_pyAddr;
		private String query_engAddr;
		private String query_modifyTime;
		private String query_modifyEmpId;
		private String query_poix;
		private String query_poiy;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.address = addressService.getAddress(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("addrId", query_addrId);
	    	    queryMap.put("regionCode", query_regionCode);
	    	    queryMap.put("addr", query_addr);
	    	    queryMap.put("addrBrief", query_addrBrief);
	    	    queryMap.put("addrNum", query_addrNum);
	    	    queryMap.put("addrLast", query_addrLast);
	    	    queryMap.put("zip", query_zip);
	    	    queryMap.put("pyAddr", query_pyAddr);
	    	    queryMap.put("engAddr", query_engAddr);
	    	    queryMap.put("modifyTime", query_modifyTime);
	    	    queryMap.put("modifyEmpId", query_modifyEmpId);
	    	    queryMap.put("poix", query_poix);
	    	    queryMap.put("poiy", query_poiy);
	    	    	
		resultList = addressService.queryAddressByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		addressService.saveAddress(address);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    if (address.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
			    	    	    if (address.getAddr().length() == 0) {
			this.addActionError("** Addr 不能为空 !");
		}
			    	    	    if (address.getAddrBrief().length() == 0) {
			this.addActionError("** AddrBrief 不能为空 !");
		}
			    	    	    	    	    	    	    	    	    	    
		this.setValidatorResult("/ndc/AddressAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    address = addressService.getAddress(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    addressService.updateAddress(address);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    if (address.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
			    			    if (address.getAddr().length() == 0) {
			this.addActionError("** Addr 不能为空 !");
		}
			    			    if (address.getAddrBrief().length() == 0) {
			this.addActionError("** AddrBrief 不能为空 !");
		}
			    			    			    			    			    		this.setValidatorResult("/ndc/AddressUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.addressService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setAddress(Address address) {
		this.address = address;
	}
	
	public Address getAddress() {
		return address;
	}
	 
	 
	 public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}
	
	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_addrId() {
        return this.query_addrId;
    }
    
    public void setQuery_addrId(String addrId){
        this.query_addrId = addrId;
    }    

        public String getQuery_regionCode() {
        return this.query_regionCode;
    }
    
    public void setQuery_regionCode(String regionCode){
        this.query_regionCode = regionCode;
    }    

        public String getQuery_addr() {
        return this.query_addr;
    }
    
    public void setQuery_addr(String addr){
        this.query_addr = addr;
    }    

        public String getQuery_addrBrief() {
        return this.query_addrBrief;
    }
    
    public void setQuery_addrBrief(String addrBrief){
        this.query_addrBrief = addrBrief;
    }    

        public String getQuery_addrNum() {
        return this.query_addrNum;
    }
    
    public void setQuery_addrNum(String addrNum){
        this.query_addrNum = addrNum;
    }    

        public String getQuery_addrLast() {
        return this.query_addrLast;
    }
    
    public void setQuery_addrLast(String addrLast){
        this.query_addrLast = addrLast;
    }    

        public String getQuery_zip() {
        return this.query_zip;
    }
    
    public void setQuery_zip(String zip){
        this.query_zip = zip;
    }    

        public String getQuery_pyAddr() {
        return this.query_pyAddr;
    }
    
    public void setQuery_pyAddr(String pyAddr){
        this.query_pyAddr = pyAddr;
    }    

        public String getQuery_engAddr() {
        return this.query_engAddr;
    }
    
    public void setQuery_engAddr(String engAddr){
        this.query_engAddr = engAddr;
    }    

        public String getQuery_modifyTime() {
        return this.query_modifyTime;
    }
    
    public void setQuery_modifyTime(String modifyTime){
        this.query_modifyTime = modifyTime;
    }    

        public String getQuery_modifyEmpId() {
        return this.query_modifyEmpId;
    }
    
    public void setQuery_modifyEmpId(String modifyEmpId){
        this.query_modifyEmpId = modifyEmpId;
    }    

        public String getQuery_poix() {
        return this.query_poix;
    }
    
    public void setQuery_poix(String poix){
        this.query_poix = poix;
    }    

        public String getQuery_poiy() {
        return this.query_poiy;
    }
    
    public void setQuery_poiy(String poiy){
        this.query_poiy = poiy;
    }    

    
	
}
